package storage

import (
	"gitee.com/alex_li/zangjingge/internal/db/rdb"
	"github.com/lishimeng/app-starter"
	"github.com/lishimeng/app-starter/persistence"
	"github.com/lishimeng/app-starter/server"
	"net/http"
)

type BucketResp struct {
	BucketCode string `json:"bucketCode,omitempty"`
	BucketName string `json:"bucketName,omitempty"`
}

func bucketList(ctx server.Context) {

	var err error
	var resp app.ResponseWrapper

	var simplePager app.SimplePager[rdb.BucketInfo, BucketResp]
	simplePager.PageSize = 10
	simplePager.PageNum = 1
	simplePager.Transform = func(src rdb.BucketInfo, dst *BucketResp) {
		dst.BucketCode = src.Code
		dst.BucketName = src.Name
	}
	simplePager.QueryBuilder = func(tx persistence.TxContext) any {
		return tx.Context.QueryTable(new(rdb.BucketInfo))
	}
	err = app.QueryPage(&simplePager)
	if err != nil {
		resp.Code = http.StatusInternalServerError
		resp.Message = err.Error()
		ctx.Json(resp)
		return
	}

	resp.Data = simplePager.Pager
	resp.Code = http.StatusOK
	ctx.Json(resp)
}
